import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import cn.jolyoulu.common.poi.excel.annotation.ExcelExport;
import cn.jolyoulu.common.poi.excel.writer.buffer.BufferExcelWriter;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;

/**
 * @Author: JolyouLu
 * @Date: 2023/12/17 21:14
 * @Description
 */
public class BufferExcelWriterTest {
    public static void main(String[] args) throws FileNotFoundException {
        ArrayList<ExpEntity1> arrayList = new ArrayList<>();
        arrayList.add(new ExpEntity1("1", "男", LocalDate.now(), 10.0, 18));
        arrayList.add(new ExpEntity1("2", "男", LocalDate.now(), 10.0, 18));
        arrayList.add(new ExpEntity1("3", "男", LocalDate.now(), 10.0, 18));
        arrayList.add(new ExpEntity1("4", "男", LocalDate.now(), 10.0, 18));
        arrayList.add(new ExpEntity1("5", "男", LocalDate.now(), 10.0, 18));

        ArrayList<ExpEntity2> arrayList2 = new ArrayList<>();
        arrayList2.add(new ExpEntity2("1", "男"));
        arrayList2.add(new ExpEntity2("2", "男"));
        arrayList2.add(new ExpEntity2("3", "男"));
        arrayList2.add(new ExpEntity2("4", "男"));
        arrayList2.add(new ExpEntity2("5", "男"));

        //测试写入数据，切换工作表，切换数据类型
        BufferExcelWriter.of(ExpEntity1.class)
                .sheet("第一个工作表")
                .write(arrayList)
                .sheet("第一个工作表",50)
                .write(arrayList)
                //切换数据
                .changeDataType(ExpEntity2.class)
                .sheet("第二个工作表")
                .write(arrayList2)
                .saveAsFile("C:\\Users\\JolyouLu\\Downloads\\","test");
        ;
        try(OPCPackage opcPackage = OPCPackage.open(new FileInputStream("C:\\Users\\JolyouLu\\Downloads\\temp.xlsx"))) {
            BufferExcelWriter.of(ExpEntity2.class)
                    .load(opcPackage)
                    .sheet("Sheet1",2)
                    .write(arrayList2)
                    .saveAsFile("C:\\Users\\JolyouLu\\Downloads\\","test");
            ;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (InvalidFormatException e) {
            throw new RuntimeException(e);
        }

    }

    @Data
    @Accessors(chain = true)
    @AllArgsConstructor
    public static class ExpEntity1 {
        @ExcelExport(titleIndex = 1, title = "姓名")
        private String name;
        @ExcelExport(titleIndex = 2, title = "性别")
        private String sex;
        @ExcelExport(titleIndex = 3, title = "生日")
        private LocalDate birthday;
        @ExcelExport(titleIndex = 4, title = "数字")
        private Double num;
        @ExcelExport(titleIndex = 5, title = "年龄")
        private Integer age;
    }

    @Data
    @Accessors(chain = true)
    @AllArgsConstructor
    public static class ExpEntity2 {
        @ExcelExport(titleIndex = 1, title = "姓名")
        private String name;
        @ExcelExport(titleIndex = 2, title = "性别")
        private String sex;
    }
}
